import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'
import NoticeList from '../views/notice/NoticeList.vue'
import ShoppingCart from '../views/ShoppingCart.vue'
import { useAuthStore } from "@/stores";
import { ElMessage } from "element-plus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component: HomeView
    },
	{
		path:'/login',
		name:'login',
		component: Login
	},
	{
		path:'/register',
		name:'register',
		component: Register
	},
	{
		path:'/notice',
		name:'allnotice',
		component: NoticeList
	},
	{
		path:'/goods',
		name:'goods',
		
		children:	[{
			path:'/goods/detail',
			name:'xiangqing',
			component: () => import('@/views/Details.vue')
			}, ]
	}, 
	{
	  path:'/types',
	  name: 'types',
	  component: () => import('@/views/TypeView.vue'),
	
	  
	},
	{
				  path:'/typeGoods',
				  name: '/typeGoods',
				  component: () => import('@/views/TypeGoods.vue')
	},
	{
    path:'/all',
    name: 'all',
    component: () => import('@/views/AllGoods.vue')
  },
  {
    path:'/custom',
    name: 'custom',
    component: () => import('@/views/Custom.vue')
  },
  {
    path:'/details',
    name: 'details',
    component:() => import('@/views/Details.vue')
  },
    {
      path: '/home',
      name: HomeView,
	  component: HomeView
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      // component: () => import('../views/AboutView.vue')
    },
     {
      path: '/shoppingCart',
      name: "ShoppingCart",
	  component: ShoppingCart
     }
  ]
})
router.beforeEach((to, from, next) => {
    //to将要访问的路径
    //from代表从哪个路径跳转而来
    //next是一个函数，表示放行
    //next() 放行 next('/login')强制跳转
    if (to.path === '/login') return next();
	if (to.path === '/home') return next();
	if (to.path === '/register') return next();
	if (to.path === '/all') return next();
	if (to.path === '/types') return next();
	if (to.path === '/typeGoods') return next();
	if (to.path === '/goods/detail') return next()
    //获取token
	const userStore = useAuthStore();
    // const tokenStr = localStorage.getItem('authorization');
    if (!userStore.isAuthenticated) {
		ElMessage.warning("请先登录~~~~")
		return next('/login');
	}
    next();
});
export default router
